package com.pfizer.Utils;

import java.util.List;

import com.pfizer.Entity.Question;
import com.pfizer.PicView.OnEndSnapListener;
import com.pfizer.pfizerExpertForPad.R;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.RadioButton;
import android.widget.TextView;

/**
 * QuestionListAdapter--<b>問卷問題的數據適配器，用於綁定listview</b>--
 * 
 * @see android.widget.BaseAdapter
 * @author krui
 * @version 1.0 2011/11/24
 */
public class QuestionListAdapter extends BaseAdapter implements OnClickListener {
	Context mContext;
	List<Question> list;
	long surveyID = 0;
	long QnID = 0;
	private OnAnswerSelectedListener asListener;
	public QuestionListAdapter() {
		// TODO Auto-generated constructor stub
	}

	/**
	 * 構造函數
	 * 
	 * @param list
	 * @param mContext
	 * @param _surveyID
	 * @param _qnID
	 */
	public QuestionListAdapter(List<Question> list, Context mContext,
			long _surveyID, long _qnID) {
		this.mContext = mContext;
		this.list = list;
		this.surveyID = _surveyID;
		this.QnID = _qnID;
	}

	/**
	 * 得到顯示數據的長度
	 */
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return list.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		QuestionViewHolder vh;
		if (convertView == null) {
			vh = new QuestionViewHolder();
			convertView = LayoutInflater.from(mContext).inflate(
					R.layout.questionlistitem, null);
			vh.tvQuestion = (TextView) convertView
					.findViewById(R.id.textQuestion);
			vh.rbChoose1 = (RadioButton) convertView.findViewById(R.id.choose1);
			vh.rbChoose2 = (RadioButton) convertView.findViewById(R.id.choose2);
			vh.rbChoose3 = (RadioButton) convertView.findViewById(R.id.choose3);
			vh.rbChoose4 = (RadioButton) convertView.findViewById(R.id.choose4);
			convertView.setTag(vh);
		} else {
			vh = (QuestionViewHolder) convertView.getTag();
		}
		// 給選項控件綁定ID，
		vh.rbChoose1.setId(position * 100 + 1);
		vh.rbChoose2.setId(position * 100 + 2);
		vh.rbChoose3.setId(position * 100 + 3);
		vh.rbChoose4.setId(position * 100 + 4);
		// 初始化選項
		// 給選項控件綁定ID，
		vh.rbChoose1.setChecked(false);
		vh.rbChoose2.setChecked(false);
		vh.rbChoose3.setChecked(false);
		vh.rbChoose4.setChecked(false);
		switch (list.get(position).getUserSctOptID()) {
		case 1:
			vh.rbChoose1.setChecked(true);
			break;
		case 2:
			vh.rbChoose2.setChecked(true);
			break;
		case 3:
			vh.rbChoose3.setChecked(true);
			break;
		case 4:
			vh.rbChoose4.setChecked(true);
			break;
		}

		// 給選項控件綁定監聽器
		vh.rbChoose1.setOnClickListener(this);
		vh.rbChoose2.setOnClickListener(this);
		vh.rbChoose3.setOnClickListener(this);
		vh.rbChoose4.setOnClickListener(this);

		vh.tvQuestion.setText(list.get(position).getNo() + ". "
				+ list.get(position).getSubj());// 问题
		switch (list.get(position).getOptionCnt()) {// 判断选择的个数
		case 2:
			vh.rbChoose1.setText(list.get(position).getOptions().get(0)
					.getDesc());
			vh.rbChoose2.setText(list.get(position).getOptions().get(1)
					.getDesc());
			vh.rbChoose3.setVisibility(View.GONE);
			vh.rbChoose4.setVisibility(View.GONE);

			break;
		case 3:
			vh.rbChoose1.setText(list.get(position).getOptions().get(0)
					.getDesc());
			vh.rbChoose2.setText(list.get(position).getOptions().get(1)
					.getDesc());
			vh.rbChoose3.setText(list.get(position).getOptions().get(2)
					.getDesc());
			vh.rbChoose4.setVisibility(View.GONE);
			break;
		case 4:
			vh.rbChoose1.setText(list.get(position).getOptions().get(0)
					.getDesc());
			vh.rbChoose2.setText(list.get(position).getOptions().get(1)
					.getDesc());
			vh.rbChoose3.setText(list.get(position).getOptions().get(2)
					.getDesc());
			vh.rbChoose4.setText(list.get(position).getOptions().get(3)
					.getDesc());
			break;
		default:
			break;
		}

		return convertView;
	}

	public void clearUserSelected() {
		int iLen = list.size();
		Question question;
		for (int i = 0; i < iLen; i++) {
			question = list.get(i);
			question.setUserSctOptID(-1);
		}
	}

	/**
	 * 處理用戶選擇結果
	 * 
	 * @return
	 */
	public int getUserSelectedCount() {
		int iCount = 0;
		int iLen = list.size();
		Question question;
		for (int i = 0; i < iLen; i++) {
			question = list.get(i);
			if (question.getUserSctOptID() != -1) {
				iCount++;
			}
		}
		return iCount;
	}

	public String getUserSelectResult() {
		int iLen = list.size();
		String sTmp = "";
		Question question;
		for (int i = 0; i < iLen; i++) {
			question = list.get(i);
			sTmp += String.format("%d::%s::%d|*|", question.getID(), question
					.getAnswerType(), question.getUserSctOptID());
		}
		if (sTmp.length() > 0) {
			sTmp = sTmp.substring(0, sTmp.length() - 3);
		}
		return sTmp;
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		int Id = v.getId();
		int optionID = Id % 100;
		int position = Id / 100;
		list.get(position).setUserSctOptID(optionID);
		if(asListener!=null) asListener.OnAnswerSelected();
	}
	
	public void setOnAnswerSelectedListener(OnAnswerSelectedListener click) {
		this.asListener = click;
	}
	public long getSurveyID() {
		return this.surveyID;
	}

	public long getQnID() {
		return this.QnID;
	}
}
